function uploadFile(file, allowedTypes, maxSize, url) {
    if (!allowedTypes.includes(file.type)) {
        alert("不支持的文件类型！");
        return;
    }

    if (file.size > maxSize) {
        alert("文件大小超出限制！");
        return;
    }

    const progressBar = document.getElementById("progressBar");
    const cancelBtn = document.getElementById("cancelBtn");

    const formData = new FormData();
    formData.append("file", file);

    // 在函数内部定义 xhr 变量
    const xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);

    // 显示上传进度条
    xhr.upload.onprogress = function (event) {
        if (event.lengthComputable) {
            const percentComplete = (event.loaded / event.total) * 100;
            progressBar.style.width = percentComplete + "%";
        }
    };

    xhr.onload = function () {
        if (xhr.status === 200) {
            alert("上传成功！");
            progressBar.style.width = "100%";
            cancelBtn.style.display = "none";
        } else {
            alert("上传失败！");
        }
    };

    xhr.onerror = function () {
        alert("上传错误！");
    };

    xhr.send(formData);

    // 显示取消按钮
    cancelBtn.style.display = "inline-block";
    cancelBtn.onclick = function () {
        xhr.abort();
        alert("上传已取消！");
        progressBar.style.width = "0%";
        cancelBtn.style.display = "none";
    };
}
